Add format option to subrip. Enable regression test for that format.
authorrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 8 Jun 2014 20:07:39 +0000 (20:07 +0000)
committerrobertlipe <robertlipe@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 8 Jun 2014 20:07:39 +0000 (20:07 +0000)
gpsbabel/reference/track/gpx_subsecond-sample.gpx
gpsbabel/reference/track/gpx_subsecond-sample~subrip.srt
gpsbabel/subrip.cc
gpsbabel/testo.d/subrip.test

index a0a4147f1325c3e78c95cfccb97cc246b45d026b..718ef24e76940ff667012290ffaa595e224e23d1 100644 (file)
@@ -11,7 +11,7 @@
 <trkseg>
 <trkpt lat="49.794688000" lon="9.834015000">
   <ele>289.200000</ele>
-  <time>2011-07-02T17:47:25Z</time>
+  <time>2011-07-02T17:47:25.2Z</time>
   <speed>13.818100</speed>
   <name>trkpt-2011-07-02T17:47:25.000Z</name>
   <type>T</type>
@@ -19,7 +19,7 @@
 </trkpt>
 <trkpt lat="49.794710000" lon="9.833996000">
   <ele>289.300000</ele>
-  <time>2011-07-02T17:47:25Z</time>
+  <time>2011-07-02T17:47:25.4Z</time>
   <speed>14.049800</speed>
   <name>trkpt-2011-07-02T17:47:25.000Z</name>
   <type>T</type>
@@ -27,7 +27,7 @@
 </trkpt>
 <trkpt lat="49.794734000" lon="9.833976000">
   <ele>289.300000</ele>
-  <time>2011-07-02T17:47:25Z</time>
+  <time>2011-07-02T17:47:25.6Z</time>
   <speed>14.161100</speed>
   <name>trkpt-2011-07-02T17:47:25.000Z</name>
   <type>T</type>
@@ -35,7 +35,7 @@
 </trkpt>
 <trkpt lat="49.794758000" lon="9.833955000">
   <ele>289.400000</ele>
-  <time>2011-07-02T17:47:25Z</time>
+  <time>2011-07-02T17:47:25.8Z</time>
   <speed>14.090400</speed>
   <name>trkpt-2011-07-02T17:47:25.000Z</name>
   <type>T</type>
 </trkpt>
 <trkpt lat="49.794782000" lon="9.833934000">
   <ele>289.500000</ele>
-  <time>2011-07-02T17:47:25Z</time>
+  <time>2011-07-02T17:47:26Z</time>
   <speed>14.183800</speed>
   <name>trkpt-2011-07-02T17:47:25.000Z</name>
   <type>T</type>
   <fix>3d</fix>
 </trkpt>
-<!--
-<trkpt lat="49.794807000" lon="9.833916000">
-  <ele>289.600000</ele>
-  <time>2011-07-02T17:47:26Z</time>
-  <speed>14.208100</speed>
-  <name>trkpt-2011-07-02T17:47:26.000Z</name>
-  <type>T</type>
-  <fix>3d</fix>
-</trkpt>
-<trkpt lat="49.794831000" lon="9.833898000">
-  <ele>289.800000</ele>
-  <time>2011-07-02T17:47:26Z</time>
-  <speed>14.272600</speed>
-  <name>trkpt-2011-07-02T17:47:26.000Z</name>
-  <type>T</type>
-  <fix>3d</fix>
-</trkpt>
-<trkpt lat="49.794856000" lon="9.833881000">
-  <ele>289.800000</ele>
-  <time>2011-07-02T17:47:26Z</time>
-  <speed>14.279100</speed>
-  <name>trkpt-2011-07-02T17:47:26.000Z</name>
-  <type>T</type>
-  <fix>3d</fix>
-</trkpt>
-<trkpt lat="49.794880000" lon="9.833863000">
-  <ele>289.800000</ele>
-  <time>2011-07-02T17:47:26Z</time>
-  <speed>14.085500</speed>
-  <name>trkpt-2011-07-02T17:47:26.000Z</name>
-  <type>T</type>
-  <fix>3d</fix>
-</trkpt>
-<trkpt lat="49.794904000" lon="9.833847000">
-  <ele>289.800000</ele>
-  <time>2011-07-02T17:47:26Z</time>
-  <speed>12.700900</speed>
-  <name>trkpt-2011-07-02T17:47:26.000Z</name>
-  <type>T</type>
-  <fix>3d</fix>
-</trkpt>
--->
 </trkseg>
 </trk>
 </gpx>
index e44b07ed38f6f05095891074dbc6c3ae54e52c56..2d69cc6ff850941247327b85b990fc08fddae7af 100644 (file)
@@ -1,24 +1,20 @@
 1
-00:00:00,000 --> 00:00:00,200
-0 km/h, 289 m
+17:47:25,200 --> 17:47:25,400 --> 49.7 km/h  289 m
 17:47:25 Lat=49.79469 Lon=9.83402
 
 2
-00:00:00,200 --> 00:00:00,400
-289 m
+17:47:25,400 --> 17:47:25,600 --> 50.6 km/h  289 m
 17:47:25 Lat=49.79472 Lon=9.83400
 
 3
-00:00:00,400 --> 00:00:00,600
-289 m
+17:47:25,600 --> 17:47:25,800 --> 51.0 km/h  289 m
 17:47:25 Lat=49.79474 Lon=9.83398
 
 4
-00:00:00,600 --> 00:00:00,800
-289 m
+17:47:25,800 --> 17:47:26,000 --> 50.7 km/h  289 m
 17:47:25 Lat=49.79476 Lon=9.83396
 
 5
-00:00:00,800 --> 00:00:01,000
-290 m
-17:47:25 Lat=49.79479 Lon=9.83394
+17:47:26,000 --> 17:47:27,000 --> 51.1 km/h  289 m
+17:47:26 Lat=49.79479 Lon=9.83394
+
index d67d29e0efd0b99733d3fe99656d6ad8632cf119..dc51b1b312e2e4aeae482e38408f518959d81f35 100644 (file)
@@ -2,6 +2,7 @@
     Write points to SubRip subtitle file (for video geotagging)
 
     Copyright (C) 2010 Michael von Glasow, michael @t vonglasow d.t com
+    Copyright (C) 2014 Gleb Smirnoff, glebius @t FreeBSD d.t org
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -27,6 +28,7 @@
 static char* opt_videotime;
 static char* opt_gpstime;
 static char* opt_gpsdate;
+static char* opt_format;
 static time_t time_offset;
 static int stnum;
 static gbfile* fout;
@@ -51,36 +53,22 @@ sync_time(time_t arg_gpstime, char* arg_videotime)
   return result;
 }
 
-static time_t
-gps_to_video_time(time_t arg_gpstime)
+static QDateTime
+gps_to_video_time(QDateTime arg_gpstime)
 {
-  static time_t result;
+  QDateTime result = arg_gpstime;
   /* Converts a GPS timestamp to relative time in the video stream. */
-  result = arg_gpstime - time_offset;
+  result.addSecs(time_offset);
   return result;
 }
 
 static void
-subrip_write_duration(time_t starttime, time_t endtime)
+subrip_write_duration(QTime starttime, QTime endtime)
 {
   /* Writes start and end time for subtitle display to file. */
-  struct tm* tmptime;
-
-  tmptime = gmtime(&starttime);
-  gbfprintf(fout, "%02d:%02d:%02d,000 --> ", tmptime->tm_hour, tmptime->tm_min, tmptime->tm_sec);
-
-  tmptime = gmtime(&endtime);
-  gbfprintf(fout, "%02d:%02d:%02d,000\n", tmptime->tm_hour, tmptime->tm_min, tmptime->tm_sec);
-}
-
-static void
-subrip_write_time(time_t arg_time)
-{
-  /* Writes a timestamp to file. */
-  struct tm* tmptime;
+  gbfprintf(fout, "%02d:%02d:%02d,%03d --> ", starttime.hour(), starttime.minute(), starttime.second(), starttime.msec());
 
-  tmptime = gmtime(&arg_time);
-  gbfprintf(fout, "%02d:%02d:%02d", tmptime->tm_hour, tmptime->tm_min, tmptime->tm_sec);
+  gbfprintf(fout, "%02d:%02d:%02d,%03d --> ", endtime.hour(), endtime.minute(), endtime.second(), endtime.msec());
 }
 
 static void
@@ -89,43 +77,87 @@ subrip_prevwp_pr(const Waypoint* waypointp)
   /* Now that we have the next waypoint, we can write out the subtitle for
    * the previous one.
    */
-  time_t starttime;
-  time_t endtime;
+  QDateTime starttime;
+  QDateTime endtime;
+  char *c;
 
-  if (prevwpp->GetCreationTime().toTime_t() >= time_offset)
-    /* if this condition is not true, the waypoint is before the beginning of
-     * the video and will be ignored
-     */
-  {
+  /* If this condition is not true, the waypoint is before the beginning of
+   * the video and will be ignored
+   */
+  if (prevwpp->GetCreationTime().toTime_t() < time_offset)
+       return;
+  starttime = gps_to_video_time(prevwpp->GetCreationTime());
+  if (!waypointp) {
+    endtime = starttime.addSecs(1);
+  } else {
+    endtime = gps_to_video_time(waypointp->GetCreationTime());
+  }
+  gbfprintf(fout, "%d\n", stnum);
+  stnum++;
+  subrip_write_duration(starttime.time(), endtime.time());
+
+  for (c = opt_format; *c != '\0' ; c++) {
+    char fmt;
+
+    switch (*c) {
+    case '%':
+      fmt = *++c;   
+      is_fatal(fmt == '\0', "No character after %% in subrip format");
+      
+      switch (fmt) {
+      case 's':
+        if WAYPT_HAS(prevwpp, speed)
+           gbfprintf(fout, "%2.1f", MPS_TO_KPH(prevwpp->speed));
+        else  
+           gbfprintf(fout, "--.-");
+        break;
+      case 'e':
+        if (prevwpp->altitude != unknown_alt)  
+          gbfprintf(fout, "%4d", (int)prevwpp->altitude);
+        else
+          gbfprintf(fout, "   -");
+        break;
+      case 't':
+        {
+        QTime t = prevwpp->GetCreationTime().time();
+          gbfprintf(fout, "%02d:%02d:%02d", t.hour(), t.minute(), t.second());
+          break;
+        }
+      case 'l':
+        // The +.00005 is for rounding.
+        gbfprintf(fout, "Lat=%0.5lf Lon=%0.5lf",
+          prevwpp->latitude+.000005, prevwpp->longitude+.000005);
+        break;
+      case 'c':
+        if (prevwpp->cadence != 0)
+          gbfprintf(fout, "%3u", prevwpp->cadence);
+        else
+          gbfprintf(fout, "  -");
+        break;
+      case 'h':
+        if (prevwpp->heartrate != 0)
+          gbfprintf(fout, "%3u", prevwpp->heartrate);
+        else
+          gbfprintf(fout, "  -");
+        break;
+      }
 
-    starttime = gps_to_video_time(prevwpp->GetCreationTime().toTime_t());
-    if (!waypointp) {
-      endtime = starttime + 1;
-    } else {
-      endtime = gps_to_video_time(waypointp->GetCreationTime().toTime_t());
-    }
-    gbfprintf(fout, "%d\n", stnum);
-    stnum++;
-    subrip_write_duration(starttime, endtime);
-    if WAYPT_HAS(prevwpp, speed) {
-      gbfprintf(fout, "%d km/h",
-                (int)(MPS_TO_KPH(prevwpp->speed) + 0.5));
-    }
-    if (prevwpp->altitude != unknown_alt) {
-      if WAYPT_HAS(prevwpp, speed) {
-        gbfprintf(fout, ", ");
+      break;
+
+    case '\\':
+      fmt = *++c;
+      is_fatal(fmt == '\0', "No character after \\ in subrip format");
+      switch (fmt) {
+      case 'n':
+        gbfprintf(fout, "\n");
+        break;
       }
-      gbfprintf(fout, "%d m\n",
-                (int)(prevwpp->altitude + 0.5));
-    } else if WAYPT_HAS(prevwpp, speed) {
-      gbfprintf(fout, "\n");
+      break;
+    default:
+      gbfwrite(c, 1, 1, fout);
     }
-    subrip_write_time(prevwpp->GetCreationTime().toTime_t());
-    gbfprintf(fout, " Lat=%0.5lf Lon=%0.5lf\n",
-              prevwpp->latitude + .000005,
-              prevwpp->longitude + .000005);
-    gbfprintf(fout, "\n");
   }
+  gbfprintf(fout, "\n\n");
 }
 
 /* callback functions */
@@ -230,6 +262,7 @@ arglist_t subrip_args[] = {
   {"video_time", &opt_videotime, "Video position for which exact GPS time is known (hhmmss, default is 0:00:00)", 0, ARGTYPE_STRING, ARG_NOMINMAX },
   {"gps_time", &opt_gpstime, "GPS time at position video_time (hhmmss, default is first timestamp of track)", 0, ARGTYPE_STRING, ARG_NOMINMAX },
   {"gps_date", &opt_gpsdate, "GPS date at position video_time (hhmmss, default is first timestamp of track)", 0, ARGTYPE_STRING, ARG_NOMINMAX },
+  {"format", &opt_format, "Format for subtitles", "%s km/h %e m\n%t %l", ARGTYPE_STRING, ARG_NOMINMAX },
   ARG_TERMINATOR
 };
 
index eb8dbcb1abdc6f863daef406bca1629953e3afb3..db19fc073e99370219f6fa5ce753509c2da27ae1 100644 (file)
@@ -1,4 +1,4 @@
 rm -f ${TMPDIR}/subrip.srt
 gpsbabel -i gpx -f ${REFERENCE}/track/gpx_subsecond-sample.gpx -o subrip -F ${TMPDIR}/subrip.srt
 # FIXME: This can't work right until we move to "real" subsecond support.
-compare  ${REFERENCE}/track/gpx_subsecond-sample~subrip.srt ${TMPDIR}/subrip.srt
+compare  ${REFERENCE}/track/gpx_subsecond-sample~subrip.srt ${TMPDIR}/subrip.srt